package com.fita.admin.web.controller.query;

import com.demo2do.core.entity.result.HttpResult;
import com.fita.admin.service.DataService;
import com.fita.entity.data.Country;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(path = "/countries")
public class CountryQueryController {
	
	@Autowired
	@Qualifier("dataService")
	private DataService<Country> dataService;
	
    @RequestMapping(value = "/query", method = RequestMethod.GET, params = "code" ,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public @ResponseBody HttpResult onQuery(@RequestParam(name = "code") String chineseCountryName) {
		
		HttpResult httpResult = new HttpResult();

		chineseCountryName = "%"+chineseCountryName+"%";
		
		Country country = dataService.load(Country.class, chineseCountryName, "(chineseName LIKE :code OR englishName LIKE :code)");
		
		if (country != null) {
			
			httpResult.success().data("code", country.getCode()).data("name", country.getChineseName());
			
			return httpResult;
		}
        return httpResult.fail("400").message("查询失败");
	}

}
